<?php
ob_end_clean();
/**
 * id: music id
 * id2: file id, the element of music
 */ 
	ob_start("ob_gzhandler");
    $file = $db->selectObject('file','id='. intval($_GET['id2']));
    $id=intval($_GET['id']);
    $db->increment('musics','download_count',1,"id = {$id}");
	if ($file != null) {
		$mimetype = $db->selectObject('mimetype',"mimetype='".$file->mimetype."'");

        $filenametest = $file->directory . "/" . $file->filename;
	
    if (file_exists($filenametest)) {			
      
      // NO buffering from here on out or things break unexpectedly. - RAM
      
      // This code was lifted from phpMyAdmin, but this is Open Source, right?
      // 'application/octet-stream' is the registered IANA type but
      //        MSIE and Opera seems to prefer 'application/octetstream'
     // It seems that other headers I've added make IE prefer octet-stream again. - RAM

      $mime_type = (EXPONENT_USER_BROWSER == 'IE' || EXPONENT_USER_BROWSER == 'OPERA') ? 'application/octet-stream;' :  $file->mimetype;

      header('Content-Type: ' . $mime_type);
      header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
      header("Content-length: ".filesize($filenametest));
      header('Content-Transfer-Encoding: binary');
      header('Content-Encoding:');
      header('Content-Disposition: attachment; filename="' . $file->filename . '"');
      // IE need specific headers
      if (EXPONENT_USER_BROWSER == 'IE') {
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Pragma: public');
		header('Vary: User-Agent');
      } else {
        header('Pragma: no-cache');
      }
      //Read the file out directly
      readfile($filenametest);
      exit();
} else {
            echo SITE_404_HTML;
         }
	} else {
		echo SITE_404_HTML;
	}
?>
